<%@page import="jwj8508.j2ee.basic.util.jdbc.MariaDB"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.Context"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>데이터베이스 커넥션 풀</title>
</head>
<body>
<h1>데이터베이스 커넥션 풀</h1>
<%
	String sql = "select * from zipcode order by seq desc limit 0, 100";
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	
	try {
		// 1. JNDI 환경 설정
		// 컴퓨터 상의 자원을 java 언어를 이용해서 사용하기 위해 context를 설정함
		Context initCtx = new InitialContext();
		Context ctx = (Context)initCtx.lookup("java:comp/env");
		
		// 2. 데이터베이스 서버를 JNDI로 호출
		DataSource ds = (DataSource)ctx.lookup("jdbc/zipcode");
		
		// 3. 커넥션 풀로 부터 연결객체를 할당받음
		conn = ds.getConnection();
		
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		
		while(rs.next()) {
			out.print(rs.getString(1) + " ");
		}
	
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		MariaDB.closeConn(conn, pstmt, rs);
	}
%>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>


</body>
</html>